import Vue from 'vue'
import app from "./app.vue"
import "./lib/mui/css/mui.min.css"
import "./lib/mui/css/icons-extra.css"

import router from './router'
import VueResource from "vue-resource"
Vue.use(VueResource )
import moment from 'moment'
Vue.filter('dateFormat',(dataStr,pattern="YYYY-MM-DD HH:mm:ss")=>{
    return moment(dataStr).format(pattern)
})
// import {Header, Swipe, SwipeItem,Button} from 'mint-ui'
// Vue.component(Header.name, Header)
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);
import MintUI from 'mint-ui'
Vue.use(MintUI)
import 'mint-ui/lib/style.css'
import VuePreview from 'vue-preview'
import Vuex from "vuex"
Vue.use(Vuex)
var car=JSON.parse(localStorage.getItem("car")||'[]')
var store=new Vuex.Store({
    state:{
        car:car
    },
    getters:{
       allCount(state){
           var c=0;
           state.car.forEach(item=>{
               c+=item.count
           })
           return c
       },
       getGoodsCount(state){
           var c={}
           state.car.forEach(item=>{
               c[item.id]=item.count
           })
           return c
       },
       getGoodsSelected(state){
           var c={}
           state.car.forEach(item=>{
            c[item.id]=item.select
        })
        return c
       },
       getGoodsTotal(state){
           var t={
               amount:0,
               count:0
           }
           state.car.forEach(item=>{
               if(item.select){
                   t.amount+=item.price*item.count
                   t.count+=item.count
               }
           })
           return t
       }
    },
    mutations:{
        addToCar(state,goodsinfo){
            console.log(1111)
            var flag=false
            state.car.some(item=>{
                if(item.id==goodsinfo.id){
                    item.count+=goodsinfo.count
                    flag=true
                    return true
                }
            
            })
            if(!flag){
                state.car.push(goodsinfo)
            }
            localStorage.setItem("car",JSON.stringify(state.car))
        },
        changeCar(state,goodsinfo){
            state.car.some(item=>{
                if(goodsinfo.id==item.id){
                    item.count=parseInt(goodsinfo.count)
                    return true
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        removeCar(state,id){
            state.car.some((item,i)=>{
                if(item.id==id){
                    state.car.splice(i,1)
                    return true
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        changeStatus(state,info){
            state.car.some(item=>{
                if(info.id==item.id){
                    item.select=info.select
                    return true
                }
            })
            localStorage.setItem('car', JSON.stringify(state.car))
        }

    },

})
Vue.use(VuePreview)
var vm =new Vue({
    el:"#app",
    render:c=>c(app),
    router,
    store
})